package com.tianbo.elfii.client.common;

import java.io.*;
import java.sql.* ;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;

import javax.imageio.*;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;

/**  <pre>
     <b>类功能描述：</b>
     完成 Config-软件更新 部分功能要求。
     </pre>
     @version 1.0.1
     @author 高伟 
*/

public class ElfiiCommonConfigUpdate {
    JPanel configPanel;
    JPanel itemPanel;

    int infoBgStartX0;
    int infoBgStartY0;

    int dataChoiceIndex;

    boolean isAboutMachineSelected=false;
    boolean isWifiSettingSelected=false;

    JFrame mainFrame;
    ElfiiCommonRunInfo runInfo;

    ItemButtonPanel autoUpdatePanel;
    ItemButtonPanel manuUpdatePanel;


    final int Id_autoUpdatePanel=1;
    final int Id_manuUpdatePanel=2;

    int selectedId;

    JDialog autoUpdateWindow ;

    public void setRunInfo(ElfiiCommonRunInfo runInfo){
        this.runInfo=runInfo;
        this.mainFrame=runInfo.getMainFrame();
    }

    public void dealBackButton(){
        mainFrame.getContentPane().removeAll();
        ElfiiCommonConfig config = new ElfiiCommonConfig();
        config.setRunInfo(runInfo);
        JPanel p=config.createViewPanel();
        mainFrame.getContentPane().setLayout(new BorderLayout());
        mainFrame.getContentPane().add(p,BorderLayout.CENTER);
        mainFrame.validate();
    }

    public void dealOkButton(){

        if (selectedId==Id_manuUpdatePanel) {
        	
        	ManualUpdatePane manualUpdatePane = new ManualUpdatePane();
        	
        	manualUpdatePane.contentPane = mainFrame.getContentPane();
        	
        	mainFrame.setContentPane(manualUpdatePane);
        	
        	mainFrame.validate();

//            JButton addWifiYesButton=new JButton();
//            ImageIcon backIcon = new ImageIcon("resource-common/images/login/sssss06.png","");
//            addWifiYesButton.setBackground(Color.white);
//            addWifiYesButton.setBorderPainted(false);
//            addWifiYesButton.setFocusPainted(false);
//            addWifiYesButton.setIcon(backIcon);
//            addWifiYesButton.addActionListener( new ActionListener() {
//                    public void actionPerformed(ActionEvent e) {
//                        autoUpdateWindow.dispose();
//                    }
//                });
//
//            addWifiYesButton.setBackground(Color.darkGray);
//            addWifiYesButton.setContentAreaFilled(false);
//
//            JButton addWifiCancelButton=new JButton();
//            ImageIcon backIcon2 = new ImageIcon("resource-common/images/login/sssss07.png","");
//            addWifiCancelButton.setBackground(Color.white);
//            addWifiCancelButton.setBorderPainted(false);
//            addWifiCancelButton.setFocusPainted(false);
//            addWifiCancelButton.setIcon(backIcon2);
//            addWifiCancelButton.addActionListener( new ActionListener() {
//                    public void actionPerformed(ActionEvent e) {
//                        autoUpdateWindow.dispose();
//                    }
//                });
//
//            addWifiCancelButton.setContentAreaFilled(false);
//
//            autoUpdateWindow=new JDialog(ElfiiCommonRunInfo.getRunInfo().getMainFrame(),true);
//            autoUpdateWindow.setUndecorated(true);		  
//            autoUpdateWindow.setBackground(null);
//
//            int x0=(int) mainFrame.getLocationOnScreen().getX();
//            int y0=(int) mainFrame.getLocationOnScreen().getY();
//
//            autoUpdateWindow.setBounds(x0,y0,1024,768);
//            autoUpdateWindow.getContentPane().setLayout(null);
//
//            AutoUpdateWindowPanel p= new AutoUpdateWindowPanel();
//            p.setLayout(null);
//            p.add(addWifiYesButton);
//            addWifiYesButton.setBounds(350,450,121,43);
//            p.add(addWifiCancelButton);
//            addWifiCancelButton.setBounds(550,450,121,43);
//            autoUpdateWindow.getContentPane().add(p);
//            p.setBounds(0,0,1024,768);
//
//            autoUpdateWindow.validate();
//            autoUpdateWindow.setVisible(true);
        }
    }

    class AutoUpdateWindowPanel extends JPanel {

        BufferedImage infoImage;
        BufferedImage bgImg;

        public AutoUpdateWindowPanel(){
            File f;
            try {
                f=new File("resource-common/images/config/icons/tb.png");
                infoImage=ImageIO.read(f);

                int x0=(int) mainFrame.getLocationOnScreen().getX();
                int y0=(int) mainFrame.getLocationOnScreen().getY();
                Robot rbt = new Robot();
                bgImg=rbt.createScreenCapture(new Rectangle(x0,y0,1024,768));

            } catch(Exception e) {System.out.print(e);}
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));   
            g2.drawImage(bgImg,null,0,0);
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));   

            int rectX0=242,rectY0=220;
            int rectWidth=530,rectHeight=280;

            g2.setColor(Color.white);
            g2.fillRoundRect(rectX0,rectY0,rectWidth,rectHeight,10,10);
            g2.setColor(Color.black);
            g2.fillRoundRect(rectX0+5,rectY0+5,rectWidth-10,rectHeight-10,10,10);
            g2.drawImage(infoImage,null,rectX0+30,rectY0+20);
            g2.setColor(Color.gray);
            g2.drawLine(rectX0+20,rectY0+85,rectX0+rectWidth-20,rectY0+85);
            g2.fillRect(rectX0+5,rectY0+rectHeight-55,rectWidth-10,50);

            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,30));
            g2.setColor(Color.white);
            g2.drawString("您的系统为最新版本",rectX0+115,rectY0+60);
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,16));
            g2.drawString("上次检查更新的日期：5月31日",rectX0+110,rectY0+150);

        }
        public Dimension getPreferredSize() {
            return (new Dimension(300,100));
        }
    }


    public void createConfigPanel(){

        JButton aboutMachineButton=new JButton("软件更新");
        aboutMachineButton.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,15));
        aboutMachineButton.setBackground(Color.gray);
        aboutMachineButton.setForeground(Color.white);
        aboutMachineButton.setBorderPainted(false);
        aboutMachineButton.setFocusPainted(false);
        aboutMachineButton.setHorizontalAlignment(SwingConstants.LEFT);
        aboutMachineButton.setModel(new DefaultButtonModel() {
                public boolean isPressed(){
                    return false; 
                }
            }
            ) ;


        autoUpdatePanel=new ItemButtonPanel("自动更新","后台自动更新应用程序");
        autoUpdatePanel.setFocusable(true);
        autoUpdatePanel.setBackground(Color.black);
        autoUpdatePanel.setSwitchStat(true);
        autoUpdatePanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                	if(e.getPoint().x>900)
                    setSelectButton(Id_autoUpdatePanel);
                }
            }
            );

        manuUpdatePanel=new ItemButtonPanel("手动更新","手动更新应用程序");
        manuUpdatePanel.setFocusable(true);
        manuUpdatePanel.setBackground(Color.black);
        manuUpdatePanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_manuUpdatePanel);
                }
            }
            );

        configPanel=new JPanel();
        configPanel.setBackground(Color.black);
        configPanel.setLayout(null);

        configPanel.add(aboutMachineButton);
        aboutMachineButton.setBounds(0,10,1024,25);

        configPanel.add(autoUpdatePanel);
        autoUpdatePanel.setBounds(0,45,1024,75);
        configPanel.add(manuUpdatePanel);
        manuUpdatePanel.setBounds(0,120,1024,75);


    }

    public void setSelectButton(int selectedId){
        this.selectedId=selectedId;
//        autoUpdatePanel.setSelected(false);
        manuUpdatePanel.setSelected(false);

		if (selectedId == Id_autoUpdatePanel){
			if(autoUpdatePanel.isSelected){
				autoUpdatePanel.setSelected(false);
				autoUpdatePanel.str2 = "已关闭";
//				wifiSettingPanel.setDefaultColor(Color.gray);
			}
			else{
				autoUpdatePanel.setSelected(true);
				autoUpdatePanel.str2 = "已开启";
//				wifiSettingPanel.setDefaultColor(Color.white);
			}
		}
        if (selectedId==Id_manuUpdatePanel)
            manuUpdatePanel.setSelected(true);

    }

    class ItemButtonPanel extends JPanel {
        BufferedImage selectedBgImage;

        BufferedImage lineImage;

        BufferedImage img2;

        BufferedImage gaoImg;
        
		BufferedImage switchOnImage;

		BufferedImage switchOffImage;
		
		boolean isSwitch;

        String str1;
        String str2;
        boolean isSelected;
        
        Color defaultColor;
        
        public ItemButtonPanel(String str1,
                               String str2) {
            this.str1=str1;
            this.str2=str2;
            File f=null;
            try {
                f=new File("resource-common/images/config/icons/tc_butbg.png");
                selectedBgImage=ImageIO.read(f);
                f=new File("resource-common/images/config/icons/tb_bg.png");
                img2=ImageIO.read(f);
                f=new File("resource-common/images/config/icons/line.png");
                lineImage=ImageIO.read(f);
                
				f = new File("resource-common/images/config/icons/switchOn.png");
				switchOnImage = ImageIO.read(f);
				f = new File(
						"resource-common/images/config/icons/switchOff.png");
				switchOffImage = ImageIO.read(f);

				isSwitch = false;
				defaultColor = Color.white;
                
            } catch(Exception e) {System.out.print(e+"   "+f);}
        }
        
		public void setSwitchStat(boolean isSwitchOn) {
			isSwitch = true;
			if (isSwitchOn) {
				this.setSelected(true);
			} else {
				this.setSelected(false);
			}
		}

        public void setSelected(boolean isSelected){
            this.isSelected=isSelected;
            repaint();
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
            g2.setColor(Color.black);
            g2.fillRect(0,0,1024,75);
            
			if (isSelected) {
				if (!isSwitch) {
					g2.drawImage(selectedBgImage, null, 0, 0);
					g2.setColor(ElfiiCommonConstant.Elfii_Blue);
				}
				else{
					g2.drawImage(switchOnImage, null, 0, 0);
					g2.setColor(defaultColor);
				}
			} else {
				if(isSwitch){
					g2.drawImage(switchOffImage, null, 0, 0);
				}
				g2.setColor(defaultColor);
				
			}

            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,20));
            g2.drawString(str1,20,25);  
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,14));
            g2.drawString(str2,20,50);  
            g2.drawImage(lineImage,null,0,74);
        }

        public Dimension getPreferredSize() {
            return (new Dimension(1024,75));
        }
    }

    public JPanel createViewPanel(){
        JPanel p = new JPanel(new BorderLayout()) ;
        JButton okButton=new JButton();
        okButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealOkButton();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        JButton backButton=new JButton();
        backButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealBackButton();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        JButton homeButton=runInfo.createHomeButton();
        JPanel topPanel=ElfiiCommonUtil.createTopPanel("系统设置",backButton,homeButton);
        JPanel bottomPanel=ElfiiCommonUtil.createBottomPanelLevelSelect(okButton);
        createConfigPanel();
        p.setLayout(null);
        p.setBackground(Color.black);
        p.add(topPanel);
        topPanel.setBounds(0,0,1024,100);
        p.add(bottomPanel);
        bottomPanel.setBounds(0,668,1024,100);
        p.add(configPanel);
        configPanel.setBounds(0,101,1024,560);
        
        okButton.setVisible(true);

        return p;
    }
}
